﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class QuestionCreated : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            ProxyWrapper.UserProxy.UserDetails objUser = ((ProxyWrapper.UserProxy.UserDetails)Session["userObject"]);
            if (objUser != null)
            {
                ProxyWrapper.CommonProxyService.Questions objQuestion = new ProxyWrapper.CommonProxyService.Questions();
                string question = Request["question"].ToString();
                string tmpQType = Request["answertype"].ToString();
                string isNumeric = Request["numeric_manual"] != null ? Request["numeric_manual"].ToString() : "0";
                string options = Request["answer[]"].ToString();
                string questionId = Request["questionId"].ToString();
                objQuestion.QuestionId = Convert.ToInt32(questionId);
                objQuestion.UserId = objUser.UserId;
                objQuestion.QuestionText = question;
                objQuestion.IsNumeric = isNumeric == "1" ? true : false;
                string questionType = "";
                switch (tmpQType)
                {
                    case "manual":
                        questionType = isNumeric == "1" ? QuestionsTypes.ShortAnswerNumeric : QuestionsTypes.ShortAnswer;
                        break;
                    case "barcode":
                        questionType = isNumeric == "1" ? QuestionsTypes.ShortAnswerBarcodeNumeric : QuestionsTypes.ShortAnswerBarcode;
                        break;
                    case "option":
                        questionType = QuestionsTypes.MultipleChoiceSingleAnswer;
                        break;
                    case "dropdown":
                        questionType = QuestionsTypes.MultipleChoiceDropdown;
                        break;
                    case "checkbox":
                        questionType = QuestionsTypes.MultiChoiceMultipleAnswers;
                        break;
                    default:
                        break;
                }
                if (options.Split(',').Length > 0)
                {
                    objQuestion.AnswerOptions = options.Split(',');
                    questionType = string.Format(questionType, objQuestion.AnswerOptions.Length.ToString());
                }
                objQuestion.QuestionType = questionType;
                objQuestion.QuestionSubType = tmpQType;
                ProxyWrapper.QuestionsServiceWrapper _qWrapper = new ProxyWrapper.QuestionsServiceWrapper();
                string message = _qWrapper.AddUpdateQuestion(objQuestion);
                if (message.Trim().Equals("0"))
                {
                    Response.Redirect("Questions.aspx?msg=Question Created.", false);
                }
                else
                {
                    Response.Write("error");
                }
            }
            else
            {
                Response.Write("Your session is expired");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
}